//
//  SignUpView.m
//  AoJi
//
//  Created by MinwenYi on 15-2-6.
//  Copyright (c) 2015年 xuanheng. All rights reserved.
//

#import "SignUpView.h"
@interface SignUpView()<UITextFieldDelegate>
{
    UIView *bgview;
}
@end

@implementation SignUpView
-(id)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:CGRectMake(16,161, Mainwidth-32,84*5/2+81/2)];
    if (self) {
//        self.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"tankuang-bg.png"]];
        [self otherConfiger];
        self.backgroundColor=[UIColor colorWithRed:247/255.0 green:247/255.0 blue:247/255.0 alpha:1];
    }

    return self;
}


-(void)otherConfiger
{
    NSArray *namesArray=[NSArray arrayWithObjects:
                         @"姓    名:",
                         @"所在城市:",
                         @"电话号码:",
                         @"E-mail:",
                         @"出国意向:",nil];
    NSArray *ImgsArray=[NSArray arrayWithObjects:@"xingmin.png",@"shuozaichengshi.png",@"lianxidianhuayuaquan.png",@"mail.png",@"liuxue-yixiang.png",nil];

    
    CGRect temrect=[self getStrRect:@"出国意向:"];
    
    int imageHeight=84/2-49/2;
    
    
  
    
    
    
    UILabel *temLabel;
    
    for (int i=0; i<namesArray.count; i++) {
        UIImageView *imgview=[[UIImageView alloc]initWithFrame:CGRectMake(20, 12+42*i, imageHeight, 20)];
        [self addSubview:imgview];
        imgview.image=[UIImage imageNamed:ImgsArray[i]];
        
        UILabel *nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(viewRight(imgview), viewTop(imgview), temrect.size.width, 20)];
        nameLabel.text=namesArray[i];
        nameLabel.font=[UIFont systemFontOfSize:15];
        nameLabel.textColor=[self getCorlor:@"b2b1b1"];
        [self addSubview:nameLabel];
        
        if (i==1) {
            temLabel=nameLabel;
        }
        UILabel *bottomLine=[[UILabel alloc]initWithFrame:CGRectMake(10, 42*(i+1), viewWidth(self)-20, 1)];
        [self addSubview:bottomLine];
        bottomLine.backgroundColor=[UIColor colorWithRed:186/255.0 green:184/255.0 blue:184/255.0 alpha:1];
        
        
    }

    
    
    
    int leftGap=viewRight(temLabel);
    int itemWidth=viewWidth(self)-leftGap-10;
    
    self.name=[[UITextField alloc]initWithFrame:CGRectMake(leftGap,12, itemWidth, imageHeight)];
    self.name.placeholder=@"请输入姓名";
    [self settextFeild:_name];
    
    
    
    self.city=[[UITextField alloc]initWithFrame:CGRectMake(leftGap, 42+12, itemWidth, imageHeight)];
    self.city.placeholder=@"请输入所在城市";
    [self settextFeild:_city];
    
    self.telephone=[[UITextField alloc]initWithFrame:CGRectMake(leftGap, 12+42*2, itemWidth, imageHeight)];
    self.telephone.placeholder=@"请输入您的电话号码";
    [self settextFeild:_telephone];
    
    self.emaillAddr=[[UITextField alloc]initWithFrame:CGRectMake(leftGap, 12+42*3, itemWidth, imageHeight)];
    self.emaillAddr.placeholder=@"请输入你的邮箱地址";
    [self settextFeild:_emaillAddr];
    
    self.intrest=[[UITextField alloc]initWithFrame:CGRectMake(leftGap, 12+42*4, itemWidth, imageHeight)];
    self.intrest.placeholder=@"请输入你的出国意向";
    [self settextFeild:self.intrest];
    
    [self   addSubview:self.name];
    [self   addSubview:self.city];
    [self    addSubview:self.telephone];
    [self    addSubview:self.emaillAddr];
    [self    addSubview:self.intrest];
    
    self.name.delegate=self;
    self.city.delegate=self;
    self.telephone.delegate=self;
    self.emaillAddr.delegate=self;
    self.intrest.delegate=self;
    
    
    
    UILabel *subMit=[[UILabel alloc]initWithFrame:CGRectMake(0, 42*namesArray.count,viewWidth(self), 81/2)];
    subMit.userInteractionEnabled=YES;
    subMit.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"but.png"]];
    UIGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap2Submitt)];
    [subMit addGestureRecognizer:tap];
    subMit.textAlignment=NSTextAlignmentCenter;
    subMit.text=@"提交预约";
    subMit.textColor=[UIColor whiteColor];
    subMit.font=[UIFont systemFontOfSize:34/2];
    [self addSubview:subMit];
    
    
    
    
    
    
    
    //    @property(nonatomic,strong)UITextField *name;
    //    @property(nonatomic,strong)UITextField *city;
    //    @property(nonatomic,strong)UITextField *telephone;
    //    @property(nonatomic,strong)UITextField *emaillAddr;
    //    @property(nonatomic,strong)UITextField  *intrest;
    _name.delegate=self;
    _city.delegate=self;
    _telephone.delegate=self;
    _emaillAddr.delegate=self;
    _intrest.delegate=self;
    
}

-(void)settextFeild:(UITextField*)tem
{
    [tem setValue:[self getCorlor:@"e0dede"] forKeyPath:@"_placeholderLabel.textColor"];
    [tem setValue:[UIFont systemFontOfSize:15] forKeyPath:@"_placeholderLabel.Font"];
    

}
-(CGRect)getStrRect:(NSString *)str
{

    CGRect temrect=[str boundingRectWithSize:CGSizeMake(200, 80) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{
                                                                                                                                                                                                                            NSFontAttributeName:[UIFont systemFontOfSize:17],
                                                                                                                            
                                                                                                                             
                                                                                                                             } context:nil];
    return temrect;
    

}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


-(void)tap2Submitt
{

    
    if ([self checkInformationIllegal])
    {
        NSLog(@"checkInformationIllegal===yes");
        
        
        
        if(self.signType==TypeIterveiw)
        {
        
            NSMutableDictionary *param=[NSMutableDictionary dictionary];
            [param setObject:self.name.text forKey:@"AF_Name"];
//            [param setObject:@"测试" forKey:@"AF_Name"];
            [param setObject:self.telephone.text forKey:@"AF_Tel"];
            [param setObject:self.emaillAddr.text forKey:@"AF_Email"];
            [param setObject:self.city.text   forKey:@"AF_City"];
            [param setObject:self.intrest.text forKey:@"AF_Country"];

        //   study/lecture/enter?id=1300
        
        [YHNetwork getInformationWithReguestType:RequestTypePost WithParameters:param WithApi:[NSString stringWithFormat:@"study/lecture/enter?id=%i",self.signId] WhenSuccess:^(NSMutableDictionary *response) {
            id restej=response;
            NSLog(@"tem=%@",response);
            
            if ([[response objectForKey:@"code"] intValue]==0) {
                [self submitSuccess];
            }
            
        } WhenFailure:^(NSError *failure) {
            
        }];
        }
        else if (self.signType==TypeActivity)
        {
            //cent.dsgrd.com/study/activity/enter?id=6033
            //AF_Name=测试&AF_Tel=13107082178&AF_Email=zzw@163.com&AF_City=海淀区&AF_Country=中国
            
            
            
            
            NSMutableDictionary *param=[NSMutableDictionary dictionary];
            [param setObject:self.name.text forKey:@"AF_Name"];
            [param setObject:self.telephone.text forKey:@"AF_Tel"];
            [param setObject:self.emaillAddr.text forKey:@"AF_Email"];
            [param setObject:self.city.text   forKey:@"AF_City"];
            [param setObject:self.intrest.text forKey:@"AF_Country"];
            
            
           
            
//             [self submitSuccess];
            
            //
            [YHNetwork getInformationWithReguestType:RequestTypePost WithParameters:param WithApi:[NSString stringWithFormat:@"study/activity/enter?id=%i",self.signId] WhenSuccess:^(NSMutableDictionary *response) {
                if ([[response objectForKey:@"code"] intValue]==0) {
                    [self submitSuccess];
                }

            } WhenFailure:^(NSError *failure) {
                
            }];

        
        
        }
        
        

    }

}
#pragma mark -提交成功

-(void)submitSuccess
{
    self.hidden=YES;
    
    bgview=[[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
//    bgview.backgroundColor=[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.5];
//    bgview.backgroundColor=[UIColor redColor];
    
    [[UIApplication sharedApplication].keyWindow addSubview:bgview];
    
    UIView *successview=[[UIView alloc]initWithFrame:CGRectMake(13, 160, Mainwidth-26, 150)];
    UILabel *contentLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, viewWidth(successview)-20, 100)];
    contentLabel.text=[NSString stringWithFormat:@"%@你已预约成功",self.name.text];
    contentLabel.numberOfLines=0;
    [successview addSubview:contentLabel];
    
    [bgview addSubview:successview];
    
    UIButton *removeBtn=[[UIButton alloc]initWithFrame:CGRectMake(viewWidth(successview)/2-60/2, 100, 60,50)];
    [removeBtn setTitle:@"确定" forState:UIControlStateNormal];
    [removeBtn addTarget:self action:@selector(removeView) forControlEvents:UIControlEventTouchUpInside];
    [successview addSubview:removeBtn];

}

-(void)removeView
{
    for (UIView *tem in bgview.subviews) {
        [tem removeFromSuperview];
     
    }
    
    [bgview removeFromSuperview];
    bgview=nil;
    [self removeFromSuperview];
   
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
 return    [textField resignFirstResponder];
}



//检测要上传的信息是否合法
-(BOOL)checkInformationIllegal
{
//    if (![self isPureNumandCharacters:self.name.text])
//    {
////        [MBProgressHUD displayHudError:@"您输入成绩必须为数字"];
//        return NO;
//    }
    
    if (![self validateName:self.name.text])
    {
//        [MBProgressHUD displayHudError:@"您输入的名字必须为中文"];
        [self hudShowError:@"您输入的名字必须为中文"];
        return  NO;
    }
    
//    AOJITextField *ViewWithTextField = [textFieldArr objectAtIndex:1];
    if (![self isPureNumandCharactersAndElevenNumbers:self.telephone.text])
    {
        
//        [MBProgressHUD displayHudError:@"您输入的电话必须为数字且为11位"];
        
         [self hudShowError:@"您输入的电话必须为数字且为11位"];
        return NO;
    }
    
//    AOJITextField *ViewWithTextField2 = [textFieldArr objectAtIndex:3];
    if(![self validateEmail:self.emaillAddr.text])
    {
//        [MBProgressHUD displayHudError:@"您输入的邮箱不合法"];
         [self hudShowError:@"您输入的邮箱不合法"];
        return NO;
    }
    
    
    return YES;
}

//检验名字是否合法
-(BOOL) validateName:(NSString *)Name
{
    if (Name.length == 0)
    {
        return NO;
    } else
    {
        for(int i=0; i< [Name length];i++)
        {
            int a = [Name characterAtIndex:i];
            if( !(a > 0x4e00 && a < 0x9fff))
                //不是汉字
                return NO;
        }
    }
    
    return YES;
}


//判断必须为11位 且是数字
- (BOOL)isPureNumandCharactersAndElevenNumbers:(NSString *)string
{
//    AOJITextField *ViewWithTextField = [textFieldArr objectAtIndex:1];
    if (string.length != 11)
    {
        return NO;
    }
    else
    {
        string = [string stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];
        if(string.length > 0)
        {
            return NO;
        }
    }
    
    return YES;
}

- (BOOL)isPureNumandCharacters:(NSString *)string
{
    
    string = [string stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];
    if(string.length > 0)
    {
        return NO;
    }
    
    return YES;
}

//检验邮箱是否合法
-(BOOL) validateEmail:(NSString *)email
{
//    AOJITextField *ViewWithTextField2 = [textFieldArr objectAtIndex:3];
    if(email.length == 0)
    {
        return NO;
    }
    else
    {
        NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
        NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
        return [emailTest evaluateWithObject:email];
    }
}

-(void)hudShowError:(NSString*)str
{
    MBProgressHUD *hud=[[MBProgressHUD alloc]init];
    hud.customView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, Mainwidth, MainHeight)];
    [self addSubview:hud];
    hud.dimBackground=YES;
//    hud.color=[UIColor whiteColor];
//    hud.
    hud.labelText=str;
//    hud.labelColor=[UIColor blackColor];
    [hud show:YES];
  
    
//    hu
    [self performSelector:@selector(hideHUD:) withObject:hud afterDelay:1];

}
-(void)hideHUD:(id)param
{
    [param hide:YES];
    [param removeFromSuperview];

}


- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    self.frame=CGRectMake(16,161-60, Mainwidth-32,84*5/2+81/2);

}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    self.frame=CGRectMake(16,161, Mainwidth-32,84*5/2+81/2);

}

@end
